package com.yby6.web.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.yby6.common.core.controller.BaseController;
import com.yby6.system.service.SysMenuService;
import com.yby6.system.service.SysRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.yby6.common.constant.Constants;
import com.yby6.common.core.AjaxResult;
import com.yby6.common.core.domain.SysMenu;
import com.yby6.common.core.domain.SysUser;
import com.yby6.common.core.page.TableDataInfo;
import com.yby6.common.core.redis.RedisUtils;
import com.yby6.common.satoken.LoginHelper;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @program: yangbuyi-rbac
 * @ClassName: SysMenuController
 * @create: 2021-05-21 15:48
 * @author: Yang Shuai
 * @since： JDK1.8
 * @SysMenuController: 菜单管理$
 **/

@RestController
@RequestMapping("/system/sysMenu")
public class SysMenuController extends BaseController {

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    /**
     * 获取菜单列表(丢弃)
     *
     * @param sysMenu
     * @return
     */
    @GetMapping("list")
    public TableDataInfo list(SysMenu sysMenu) {
        return getDataTable(sysMenuService.selectMenuList(sysMenu, LoginHelper.getUser()));
    }

    /**
     * 根据ID查询菜单信息
     *
     * @param sysMenu
     * @return
     */
    @GetMapping("/{menuId}")
    public AjaxResult getMenuInfo(@PathVariable Long menuId) {
        return AjaxResult.success(sysMenuService.selectMenuById(menuId));
    }

    /**
     * 构造树形菜单列表
     *
     * @param sysMenu
     * @return
     */
    @GetMapping("/getTreeList")
    public AjaxResult getTreeList(SysMenu sysMenu) {
        return AjaxResult.success(sysMenuService.getTreeList(sysMenu, LoginHelper.getUser()));
    }


    /**
     * 根据当前登陆用户ID获取动态路由菜单
     *
     * @return
     */
    @GetMapping("/getRouters")
    public AjaxResult getRouters() {
        SysUser loginUser = LoginHelper.getUser();
        return AjaxResult.success(sysMenuService.getRouters(loginUser));
    }


    /**
     * 添加菜单
     *
     * @param sysMenu
     * @return
     */
    @PostMapping("addMenu")
    @SaCheckPermission("sys:menu:add")
    public AjaxResult addMenu(@RequestBody SysMenu sysMenu) {
        sysMenu.setCreateBy(LoginHelper.getUsername());
        return this.sysMenuService.addMenu(sysMenu);
    }

    /**
     * 修改菜单
     *
     * @param sysMenu
     * @return
     */
    @PutMapping("updateMenu")
    @SaCheckPermission("sys:menu:update")
    public AjaxResult updateMenu(@RequestBody SysMenu sysMenu) {
        sysMenu.setUpdateBy(LoginHelper.getUsername());
        return this.sysMenuService.updateMenu(sysMenu);
    }

    /**
     * 删除菜单
     *
     * @param menuId
     * @return
     */
    @DeleteMapping("/{menuId}")
    @SaCheckPermission("sys:menu:delete")
    public AjaxResult deleteMenu(@PathVariable("menuId") Long menuId) {
        if (sysMenuService.hasChildByMenuId(menuId)) {
            return AjaxResult.error("存在子菜单,不允许删除");
        }
        if (sysMenuService.checkMenuExistRole(menuId)) {
            // 先删除角色的关联表
            sysRoleMenuService.deleteByMenuId(menuId);
        }
        return toAjax(sysMenuService.deleteMenuById(menuId));
    }

    @PostConstruct
    public void loadMenu() {
        List<SysMenu> menuList = sysMenuService.lambdaQuery().select(SysMenu::getPermission).isNotNull(SysMenu::getPermission).eq(SysMenu::getStatus, Constants.ONE).list();
        RedisUtils.setCacheList(Constants.MENU_KEY, menuList);
    }

}
